﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Security;

    internal static class AssemblyUtilities
    {
        internal static IEnumerable<Type> GetExportedTypes(Assembly assembly, Action<string> logger)
        {
            Type[] typeArray = null;
            try
            {
                typeArray = (assembly == null) ? new Type[0] : assembly.GetExportedTypes();
            }
            catch (Exception exception)
            {
                if ((!(exception is TypeLoadException) && !(exception is FileNotFoundException)) && ((!(exception is FileLoadException) && !(exception is BadImageFormatException)) && !(exception is ReflectionTypeLoadException)))
                {
                    throw;
                }
                if (logger != null)
                {
                    logger(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Failed_Get_Types, new object[] { assembly.FullName, exception.Message }));
                }
                return new Type[0];
            }
            return typeArray;
        }

        internal static IEnumerable<Assembly> GetReferencedAssemblies(Assembly assembly, Action<string> logger)
        {
            AssemblyName[] nameArray = (assembly == null) ? new AssemblyName[0] : assembly.GetReferencedAssemblies();
            List<Assembly> list = new List<Assembly>();
            foreach (AssemblyName name in nameArray)
            {
                Assembly item = LoadAssembly(name, logger);
                if (item != null)
                {
                    list.Add(item);
                }
            }
            return list;
        }

        internal static Assembly LoadAssembly(AssemblyName assemblyName, Action<string> logger)
        {
            Assembly assembly = null;
            try
            {
                assembly = Assembly.Load(assemblyName);
            }
            catch (Exception exception)
            {
                if ((!(exception is FileNotFoundException) && !(exception is FileLoadException)) && ((!(exception is PathTooLongException) && !(exception is BadImageFormatException)) && !(exception is SecurityException)))
                {
                    throw;
                }
                if (logger != null)
                {
                    logger(exception.ToString());
                    logger(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Failed_Load, new object[] { assemblyName, exception.Message }));
                }
                return assembly;
            }
            return assembly;
        }

        internal static Assembly LoadAssembly(string assemblyFileName, Action<string> logger)
        {
            Assembly assembly = null;
            try
            {
                assembly = LoadAssembly(AssemblyName.GetAssemblyName(assemblyFileName), null);
                if (assembly != null)
                {
                    return assembly;
                }
                assembly = Assembly.LoadFrom(assemblyFileName);
            }
            catch (Exception exception)
            {
                if ((!(exception is FileNotFoundException) && !(exception is FileLoadException)) && ((!(exception is PathTooLongException) && !(exception is BadImageFormatException)) && !(exception is SecurityException)))
                {
                    throw;
                }
                if (logger != null)
                {
                    logger(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Failed_Load, new object[] { assemblyFileName, exception.Message }));
                }
                return assembly;
            }
            return assembly;
        }
    }
}

